package com.sumainfor.pc.market.service;
import com.qiniu.util.Json;
import com.sumainfor.common.Constants;
import com.sumainfor.common.util.ContextUtils;
import com.sumainfor.common.util.JsonResult;
import com.sumainfor.common.utlis.ComUtils;
import com.sumainfor.common.utlis.FileSubufferUtils;
import com.sumainfor.common.utlis.ParamsCommon;
import com.sumainfor.pc.market.dao.RdCorporateMapper;
import com.sumainfor.pc.market.dao.UtilsMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @Date 2018年11月7日
 * @Describe 企业客户
 */
@Service
public class RdCorporateService implements Serializable {

    private ReentrantLock lock = new ReentrantLock(false);

    Logger log = LoggerFactory.getLogger(RdCorporateService.class);// 日志

    @Autowired
    private RdCorporateMapper rdCorporateMapper;
    @Autowired
    private UtilsMapper utilsMapper;

    String UUID=null;
    /**
     * 新增企业客户
     * @param params
     * @return
 */    @Transactional(rollbackFor = Exception.class)
        public JsonResult addCorporate(Map<String, Object> params) {
            JsonResult result=new JsonResult();
            /**
             * 加锁操作
             */
            lock.lock();
            try{
                try {
                    Map<String,Object> map=(Map<String,Object>)params.get("data");
                    if (map.get("FileAnnex2")==null){
                        return result.putFailed("参数不正确");
                    }
                    List<Map<String,Object>> list=(List<Map<String,Object>>)map.get("FileAnnex2");
                    map.put("FileAnnex", FileSubufferUtils.ListTranStringUtils(list));
                    Map<String,Object>uuid=utilsMapper.getUUID(new HashMap<String,Object>(){{
                        put("uuid", "CorId");
                        put("database", Constants.DATA_SOURCE_TCMKT);
                        put("surface", "tcmkt_corporate");
                    }});
                    if(uuid!=null){
                        int number=Integer.valueOf(uuid.get("uuid").toString());
                        UUID= ComUtils.getLocalTrmSeqNum("co",number);
                    }else{
                        UUID=ComUtils.getLocalTrmSeqNum("co",0);
                    }
                    System.out.println("前端传递的参数:"+map);
                    map.put("by", ContextUtils.getUserId());//操作人
                    map.put("CorId",UUID);
                    map.put("term", "2");// 创建端 Insert端： 1 租客端 2市场部 3 工程部   4运营部   5财务部  6总经办 7发展部
                    map.put("fnc",
                            this.getClass().getSimpleName() + "." + Thread.currentThread().getStackTrace()[1].getMethodName());// 创建Function名
                    ParamsCommon.commonInsertParms(map);

                    Integer count=rdCorporateMapper.addCorporate(map);
                    if (count>0){
                        result.putSuccess("操作成功");
                    }else{
                        result.putFailed("服务器繁忙");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                }
            }
            finally {
                lock.unlock();
            }
            return result;
        }
    /**
     * 运营部服务秘书查询所有的秘书
     */
    public List<Map<String,Object>> getCorporateList(){
        return rdCorporateMapper.getCorporateList();
    }
}
